/**
 * Project Name:jh
 * File Name:RadarTCPTXThread.java
 * Package Name:com.jh.comm
 * Date:下午6:16:32
 * Copyright (c) 2021, LZ All Rights Reserved.
 *
*/

package com.jh.comm;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jh.utils.BlockingQueueUtils;
import com.jh.utils.SocketUtils;

/**
 * Description: <br/>
 * Date: 下午6:16:32 <br/>
 * 
 * @author lz E-mail:withitren@outlook.com
 * @version
 * @see
 */
public class RadarTCPTakeTXBQThread extends Thread {
    static final Logger logger = LoggerFactory.getLogger(RadarTCPTakeTXBQThread.class);

    @Override
    public void run() {
        logger.debug("--->" + "enter RadarTCPTakeTXBQThread run!");
        BlockingQueueUtils queueUtils = BlockingQueueUtils.getInstance();
        ArrayBlockingQueue<String> txBq = queueUtils.gettxBq();
        String take = null;
        Socket s = SocketUtils.getSocket();
        PrintStream ps = null;
        try {
            ps = new PrintStream(s.getOutputStream());
        } catch (IOException e1) {
            ps.close();
            logger.error(e1.getMessage(), e1);

        }
        while (true) {
            System.out.println(getName() + " RadarTCPTakeTXBQThread ready to take queue！");
            // Thread.sleep(200);
            // 尝试取出元素，如果队列为空，则线程被阻塞
            try {
                take = txBq.take();
                // 将字符串写入socket对应的输出流
                ps.println(take);
                // By using flush() method is to flush the stream immediately
                ps.flush();
                logger.debug("--->" + "take :" + take);
                System.out
                        .println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "   take:   " + take);
            } catch (InterruptedException e) {
                ps.close();
                logger.error(e.getMessage(), e);
            }
            System.out.println(getName() + " RadarTCPTakeTXBQThread finished to take queue：");
        }

    }

}
